<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    class Person {
      constructor() {
        this.queue = []
        setTimeout(() => {
          this.next()
        })
      }

      eat(str) {
        let fn = () => {
          console.log(str);
          this.next()
        }
        this.queue.push(fn)
        return this
      }

      sleep(time) {
        let fn = () => {
          console.log('sleep');
          setTimeout(() => {
            this.next()
          }, time)
        }
        this.queue.push(fn)

        return this
      }

      next() {
        let fn = this.queue.shift()
        fn && fn()
      }

    }
    let p = new Person()
    p.eat('breakfast').sleep(3000).eat('dinner')
  </script>
</body>

</html>